﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Galaktika.BI.Silverlight.Services;
using Galaktika.BI.Silverlight.Commands;
using Ranet.Olap.Core.Storage;
using Ranet.Olap.Core;
//using Ranet.Olap.Core.Compressor;
using System.Configuration;
using Ranet.ZipLib;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Commands
{
    public class StorageActionCommand : ServerCommand
    {
        bool UseCompress = true;
        public StorageActionCommand(InvokeSchema invoker)
            : base(CommandId.StorageActionCommandId)
        {
            if (ConfigurationSettings.AppSettings.AllKeys.Contains("CompressData"))
            {
                String str = ConfigurationSettings.AppSettings["CompressData"];
                try
                {
                    if (!String.IsNullOrEmpty(str))
                        UseCompress = Convert.ToBoolean(str);
                }
                catch (System.FormatException)
                {
                }
            }
            this.Schema = invoker;
            this.Args = XmlSerializationUtility.XmlStr2Obj<StorageActionArgs>(invoker[KnownInvokeArgs.StorageAction]);
        }

        public StorageActionArgs Args { get; private set; }    
        public InvokeSchema Schema { get; private set; }

        public override InvokeResultSchema Invoke()
        {
            InvokeResultSchema result = null;
            StorageActionArgs args = this.Args;
            String res = String.Empty;
            if (args != null)
            { 
                   FileStorageProvider storageProvider = new FileStorageProvider();
                    switch (args.ActionType)
                    {
                        case StorageActionTypes.Save:
                            args.FileDescription.ContentFileName = args.FileDescription.Description.Name + ".content." + FileStorageProvider.GetFilteExtension(args.ContentType);
                            storageProvider.Save(HttpContext.Current.User, args.FileDescription.Description.Name + "." + FileStorageProvider.GetFilteExtension(args.ContentType), XmlSerializationUtility.Obj2XmlStr(args.FileDescription));
                            storageProvider.Save(HttpContext.Current.User, args.FileDescription.ContentFileName, args.Content);
                            break;
                        case StorageActionTypes.Load:
                            res = storageProvider.Load(HttpContext.Current.User, args.FileDescription.ContentFileName);
                            break;
                        case StorageActionTypes.GetList:
                            res = XmlUtility.Obj2XmlStr(storageProvider.GetList(HttpContext.Current.User, "*." + FileStorageProvider.GetFilteExtension(args.ContentType)), Common.Namespace);
                            break;
                    }
            }

            result = new InvokeResultSchema();
            result.Content = res;
            if (UseCompress)
            {
                // Архивация строки
                String compesed = ZipCompressor.CompressAndConvertToBase64String(res);
                result.Content = compesed;
                result.IsArchive = true;
            }
            return result;
        }
    }
}
